﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using siqi.Protocol;
using siqi.Interfaces;
namespace siqi.Logic
{
    class Room:Interfaces.IRoom
    {
        public Room(string id, string name)
        {
            ID = id;
            Name = name;
            Desks = new List<IDesk>();
            Users = new Dictionary<string, IUserAgent>();
        }

        private  Queue<MessageToken> mQueue = new Queue<MessageToken>();

        public string ID
        {
            get;
            set;
        }

        public IList<IDesk> Desks
        {
            get;
            private set;
        }

        public void Add(IDesk desk)
        {
            // TODO: Implement this method
            desk.Room = this;
            Desks.Add(desk);
        }

        public string Password
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public IDictionary<string, IUserAgent> Users
        {
            get;
            private set;
        }

        public void In(IUserAgent user)
        {
            if (user.Desk != null)
                user.Desk.Exit(user);
            if (user.Room != null)
            {
                user.Room.Exit(user);
            }
            Users[user.ID] = user;
            user.Room = this;
        }

        public void Exit(IUserAgent user)
        {
            if (user.Room == this)
            {
                user.Desk = null;
                user.Seat = null;
                user.Room = null;
                Users.Remove(user.ID);
            }
        }

        public void AddMessage(IMessage message, IUserAgent user)
        {
            lock (this)
            {
                mQueue.Enqueue(new MessageToken { Message= message, UserAgent= user });
            }
        }

        public void Execute(ISiqiServer server)
        {
            MessageToken token = null;
            lock (this)
            {
                if (mQueue.Count > 0)
                    token = mQueue.Dequeue();
            }
            if (token != null)
                OnExecute(token, server);
        }

        protected virtual void OnExecute(MessageToken token, ISiqiServer server)
        {
            //添加处理逻辑
        }
    }
}
